<?php
/**
 * Description of UploadImageFacade
 *
 * @author Alexey
 */
Yii::import('application.extensions.image.Image');

class UploadImageFacade {

    const SIZE_IMAGE_DELIM = "__";
    const NO_PHOTO = "/images/no-photo.gif";

    public static function saveImageModel($path)
    {
        if ($path == null)
        {
            throw new CException("UploadImage path cann't be null.");
        }
        $uploadImage = new UploadImage();
        $uploadImage->path = $path;
        $uploadImage->save();
        return $uploadImage;
    }

    public static function resolveImagePath($imageId, $imageSize = null)
    {
        $imageModel = UploadImageFacade::loadModel($imageId);
        $srcImagePath = $imageModel->path;
        if ($imageSize == null)
        {
            return str_replace(Yii::getPathOfAlias('webroot'), "", $srcImagePath);
        }
        $arrSize = split("x", $imageSize);
        $width = intval($arrSize[0]);
        $height = intval($arrSize[1]);
        if ($width == 0 || $height == 0)
        {
            throw new CException("Invalid image size.");
        }
        if ( !file_exists($srcImagePath))
        {
            $srcImagePath = Yii::getPathOfAlias('webroot') . UploadImageFacade::NO_PHOTO;
        }
        $fileInfo = pathinfo($srcImagePath);
        $baseName = basename($fileInfo['basename'], "." . $fileInfo['extension']);
        $dir = dirname($srcImagePath);
        $resizedImageName = $baseName . UploadImageFacade::SIZE_IMAGE_DELIM . $imageSize . "." . $fileInfo['extension'];
        $resizedImagePath = $dir . "/" . $resizedImageName;
        if (!file_exists($resizedImagePath))
        {
            UploadImageFacade::framingImage($srcImagePath, $resizedImagePath, $width, $height);
        }
        $resizedImagePath = str_replace(Yii::getPathOfAlias('webroot'), "", $resizedImagePath);
        return $resizedImagePath;
    }

    public static function framingImage($srcPath, $resizedImagePath, $width, $height)
    {
        if ($width == 0 || $height == 0)
        {
            throw new CException("Invalid image size.");
        }
        $image = Yii::app()->image->load($srcPath);
        $widthRatio = $width / $image->image['width'];
        $heightRatio = $height / $image->image['height'];
        if ($widthRatio > $heightRatio)
        {
            $image->resize($width, $image->image['height'] * $widthRatio);
            $image->crop($width, $height);
        }
        else
        {
            $image->resize($image->image['width'] * $heightRatio, $height);
            $image->crop($width, $height);
        }
        $image->save($resizedImagePath);
    }

    public static function loadModel($id)
    {
        return UploadImage::model()->findByPk((int)$id);
    }

    public static function unsetImage($imageId)
    {
        $image = UploadImageFacade::loadModel($imageId);
        $dirname = dirname($image->path);
        $fileInfo = pathinfo($image->path);
        $baseName = basename($fileInfo['basename'], "." . $fileInfo['extension']);

        opendir($dirname);

        if ($handle = opendir($dirname)) {
            while (false !== ($filename = readdir($handle))) {
                if (strpos($filename, $baseName) !== false)
                {
                    unlink($dirname . "/" . $filename);
                }
            }
            closedir($handle);
        }
        $image->delete();
    }

}
?>
